//
//  ZWScanViewController.swift
//  SwiftFreshBeen
//
//  Created by Apple on 16/12/22.
//  Copyright © 2016年 zhouwnegang. All rights reserved.
//

import UIKit
import AVFoundation


class ZWScanViewController: UIViewController {

    @IBOutlet weak var areaImageView: UIImageView!
    @IBOutlet weak var lineImageView: UIImageView!
    
    var videoInput:AVCaptureInput?
    var output:AVCaptureMetadataOutput?
    var session:AVCaptureSession?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.定义输入设备
       let videoDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        
       self.videoInput = try? AVCaptureDeviceInput(device: videoDevice)
        //2.定义输出
        self.output = AVCaptureMetadataOutput()

        
        let queue = DispatchQueue.main
        
        self.output?.setMetadataObjectsDelegate(self, queue:queue)
        
        
        //3.关联对象
        
        self.session = AVCaptureSession()
        
        //3.1 关联输入
        if (self.session?.canAddInput(videoInput))! {
            
            self.session?.addInput(videoInput)
            
        }
        
        //3.2 关联输出
        
        if (self.session?.canAddOutput(output))! {
            
            self.session?.addOutput(output)
        }
        
        //设置输出数据类型
        self.output?.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
        
        
        //4.设置预览
        let preViewLayer = AVCaptureVideoPreviewLayer(layer: self.session!)
        preViewLayer.frame = self.view.bounds
        
        self.view.layer.insertSublayer(preViewLayer, at: 0)
        
        //5.开始运作
        self.session?.startRunning()
        
        
        //6.设置细线的动画
        setupAnimation()
        
        
    }

    
    private func setupAnimation() {
        
        self.view.layoutIfNeeded()
        
        let originPoint = self.lineImageView.layer.position
        
        let targetPoint = CGPoint(x: originPoint.x, y: self.areaImageView.frame.size.height - self.lineImageView.frame.size.height - 10)
        
        let baseAn = CABasicAnimation(keyPath: "position")
        
        baseAn.fromValue = NSValue.init(cgPoint: originPoint)
        baseAn.toValue = NSValue.init(cgPoint: targetPoint)
        
        baseAn.duration = 3.0
        baseAn.repeatCount = MAXFLOAT
        
        self.lineImageView.layer.add(baseAn, forKey: nil)
        
    }
}
  //实现代理方法
extension ZWScanViewController:AVCaptureMetadataOutputObjectsDelegate {
    
    // 对metadata数据识别后的输出
    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
        
        for meta in metadataObjects {
            
            if let url = URL(string: (meta as AnyObject).stringValue) {
                UIApplication.shared.openURL(url as URL)
                
                 print("%@",url)
            }
            
        }
        
    }
    
}
